﻿/*
***************************************************************************
* Microsoft Visual C# .NET
* 名称：       Shpm.System.Db.ShpmBaseDao
* 类型：       数据库访问对象抽象类
* 功能说明：   继承此类，实现对数据库的访问操作
* 作者：       SHPM.彭城东
* 创建日期：   2007/07/10
* 修改日期     修改者      修改内容* 
***************************************************************************
*/

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data.SqlClient;
using IBatisNet.DataMapper;
using IBatisNet.Common;
using IBatisNet.Common.Utilities;
using IBatisNet.Common.Utilities.Objects;
using IBatisNet.Common.Utilities.Objects.Members;
using IBatisNet.DataMapper.Configuration.Cache;
using IBatisNet.DataMapper.Configuration.ParameterMapping;
using IBatisNet.DataMapper.Configuration.ResultMapping;
using IBatisNet.DataMapper.DataExchange;
using IBatisNet.DataMapper.Exceptions;
using IBatisNet.DataMapper.MappedStatements;
using IBatisNet.DataMapper.SessionStore;
using IBatisNet.DataMapper.TypeHandlers;
using log4net;

using IBatisNet.DataMapper.Configuration;
using System.Reflection;
using System.Collections.Specialized;

namespace Shpm.System.Db
{
    public abstract class  ComBaseDao 
    {
        // protected static ILog log = LogManager.getLogger(typeof(ShpmBaseDao));

        // protected static SqlMapper sqlMapClient = null;

        /*    
        ************************************************
        * 类属性定义
        ************************************************
        */

        // The factory which build IAccessor
        protected AccessorFactory accessorFactory;

        // Factory for DataExchange objects 
        protected DataExchangeFactory dataExchangeFactory;

        // The DataSource 
        protected DataSource dataSource;

        // Name used to identify the the SqlMapper
        protected string id;

        // A flag that determines whether cache models were enabled when this SqlMap was built. 
        protected Boolean isCacheModelsEnabled;

        // Gets a value indicating whether this instance is session started. 
        protected Boolean isSessionStarted;

        // Returns the DalSession instance currently being used by the SqlMap. 
        protected ISqlMapSession localSession;

        // The MappedStatements collection 
        protected HybridDictionary mappedStatements;

        // The meta factory for object factory 
        protected IObjectFactory objectFactory;

        // The ParameterMap collection 
        protected HybridDictionary parameterMaps;

        // The ResultMap collection 
        protected HybridDictionary resultMaps;

        // Allow to set a custom session store like the HybridWebThreadSessionStore 
        protected ISessionStore sessionStore;

        // The TypeHandlerFactory 
        protected TypeHandlerFactory typeHandlerFactory;

        protected AccessorFactory AccessorFactory 
        {
            get
            {
                return accessorFactory;
            }
        }

        protected DataExchangeFactory DataExchangeFactory
        {
            get
            {
                return dataExchangeFactory;
            }
        }

        protected DataSource DataSource
        {
            get
            {
                return dataSource;
            }
            set
            {
                this.dataSource = value;
            }
        }

        protected string Id
        {
            get
            {
                return id;
            }
        }

        protected Boolean IsCacheModelsEnabled
        {
            get
            {
                return isCacheModelsEnabled;
            }
            set
            {
                this.isCacheModelsEnabled = value;
            }
        }

        protected Boolean IsSessionStarted
        {
            get
            {
                return isSessionStarted;
            }
        }

        protected ISqlMapSession LocalSession
        {
            get
            {
                return localSession;
            }
        }

        protected HybridDictionary MappedStatements
        {
            get
            {
                return mappedStatements;
            }
        }

        protected IObjectFactory ObjectFactory
        {
            get
            {
                return objectFactory;
            }
        }

        protected HybridDictionary ParameterMaps
        {
            get
            {
                return parameterMaps;
            }
        }

        protected HybridDictionary ResultMaps
        {
            get
            {
                return resultMaps;
            }
        }

        protected ISessionStore SessionStore
        {
            set
            {
                this.sessionStore = value;
            }
        }

        protected TypeHandlerFactory TypeHandlerFactory
        {
            get
            {
                return typeHandlerFactory;
            }
        }


        //protected SqlMapper getSqlMapClient()
        //{
        //    if (sqlMapClient == null)
        //    {
        //        lock (typeof(SqlMapper))
        //        {
        //            if (sqlMapClient == null)
        //            {					    
        //                try
        //                {
        //                    DomSqlMapBuilder build = new DomSqlMapBuilder();
        //                    sqlMapClient = (SqlMapper)build.Configure();
        //                }
        //                catch(Exception e) {
        //                    sqlMapClient = null;
        //                    getLogger().Fatal(e);
        //                    throw new DataAccessException("The configuration file is invalid." + e.Message, e);
        //                }
        //            }
        //        }
        //    }
        //    return sqlMapClient;
        //}

        protected abstract SqlMapper getSqlMapClient();
        protected abstract ILog getLogger();

        /*
        *名称： addCache
        *功能： Adds a (named) cache
        *参数： arg0
        *返回： void
        *说明： 
        */
        protected void AddCache(CacheModel arg0)
        {
            try
            {
                this.getSqlMapClient().AddCache(arg0);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： addMappedStatement
        *功能： Adds a (named) MappedStatement
        *参数： arg0 , arg1
        *返回： void
        *说明： 
        */
        protected void AddMappedStatement(string arg0, IMappedStatement arg1)
        {
            try
            {
                this.getSqlMapClient().AddMappedStatement(arg0,arg1);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： addParameterMap
        *功能： Adds a (named) ParameterMap
        *参数： arg0
        *返回： void
        *说明： 
        */
        protected void AddParameterMap(ParameterMap arg0)
        {
            try
            {
                this.getSqlMapClient().AddParameterMap(arg0);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： addResultMap
        *功能： Adds a (named) ResultMap 
        *参数： arg0
        *返回： void
        *说明： 
        */
        protected void AddResultMap(IResultMap arg0)
        {
            try
            {
                this.getSqlMapClient().AddResultMap(arg0);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： beginTransaction
        *功能： Overloaded. Begins a database transaction. 
        *参数： none
        *返回： ISqlMapSession
        *说明： 
        */
        protected ISqlMapSession BeginTransaction()
        {
            try
            {
                return this.getSqlMapClient().BeginTransaction();
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： beginTransaction
        *功能： Overloaded. Begins a database transaction. 
        *参数： arg0(Boolean)
        *返回： ISqlMapSession
        *说明： 
        */
        protected ISqlMapSession BeginTransaction(Boolean arg0)
        {
            try
            {
                return this.getSqlMapClient().BeginTransaction(arg0);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： beginTransaction
        *功能： Overloaded. Begins a database transaction. 
        *参数： arg0(string)
        *返回： ISqlMapSession
        *说明： 
        */
        protected ISqlMapSession BeginTransaction(string arg0)
        {
            try
            {
                return this.getSqlMapClient().BeginTransaction(arg0);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： closeConnection
        *功能： Close a connection 
        *参数： 
        *返回： 
        *说明： 
        */
        protected void CloseConnection()
        {
            try
            {
                this.getSqlMapClient().CloseConnection();
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： createSqlMapSession
        *功能： Overloaded. Creates a new SqlMapSession that will be used to query the data source. 
        *参数： 
        *返回： ISqlMapSession
        *说明： 
        */
        protected ISqlMapSession CreateSqlMapSession()
        {
            try
            {
                return this.getSqlMapClient().CreateSqlMapSession();
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： createSqlMapSession
        *功能： Overloaded. Creates a new SqlMapSession that will be used to query the data source. 
        *参数： arg0
        *返回： ISqlMapSession
        *说明： 
        */
        protected ISqlMapSession CreateSqlMapSession(string arg0)
        {
            try
            {
                return this.getSqlMapClient().CreateSqlMapSession(arg0);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： delete
        *功能： Executes a Sql DELETE statement. Delete returns the number of rows effected. 
        *参数： arg0 , arg1
        *返回： int
        *说明： 
        */
        protected int Delete(string arg0, Object arg1)
        {
            try
            {
                return getSqlMapClient().Delete(arg0, arg1);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： delete
        *功能： Executes a Sql DELETE statement. Delete returns the number of rows effected. 
        *参数： arg0
        *返回： int
        *说明： 
        */
        protected int Delete(string arg0)
        {
            try
            {
                return getSqlMapClient().Delete(arg0, null);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： flushCaches
        *功能： Flushes all cached objects that belong to this SqlMap 
        *参数： 
        *返回： 
        *说明： 
        */
        protected void FlushCaches()
        {
            try
            {
                this.getSqlMapClient().FlushCaches();
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： getCache
        *功能： Gets a cache by name 
        *参数： arg0
        *返回： CacheModel
        *说明： 
        */
        protected CacheModel GetCache(string arg0)
        {
            try
            {
                return this.getSqlMapClient().GetCache(arg0);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： getDataCacheStats
        *功能： 
        *参数： 
        *返回： string
        *说明： 
        */
        protected string GetDataCacheStats()
        {
            try
            {
                return this.getSqlMapClient().GetDataCacheStats();
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： getMappedStatement
        *功能： Gets a MappedStatement by name 
        *参数： arg0
        *返回： IMappedStatement
        *说明： 
        */
        protected IMappedStatement GetMappedStatement(string arg0)
        {
            try
            {
                return this.getSqlMapClient().GetMappedStatement(arg0);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： getParameterMap
        *功能： Get a ParameterMap by name 
        *参数： arg0
        *返回： ParameterMap
        *说明： 
        */
        protected ParameterMap GetParameterMap(string arg0)
        {
            try
            {
                return this.getSqlMapClient().GetParameterMap(arg0);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： getResultMap
        *功能： Gets a ResultMap by name 
        *参数： arg0
        *返回： IResultMap
        *说明： 
        */
        protected IResultMap GetResultMap(string arg0)
        {
            try
            {
                return this.getSqlMapClient().GetResultMap(arg0);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： insert
        *功能： Executes a Sql INSERT statement. Insert is a bit different from other update methods,
        *       as it provides facilities for returning the primary key of the newly inserted row 
        *       (rather than the effected rows). This functionality is of course optional. The parameter
        *       object is generally used to supply the input data for the INSERT values. 
        *参数： arg0 , arg1
        *返回： Object
        *说明： 
        */
        protected Object Insert(string arg0, Object arg1)
        {
            try
            {
                return getSqlMapClient().Insert(arg0, arg1);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： insert
        *功能： Executes a Sql INSERT statement. Insert is a bit different from other update methods,
        *       as it provides facilities for returning the primary key of the newly inserted row 
        *       (rather than the effected rows). This functionality is of course optional. The parameter
        *       object is generally used to supply the input data for the INSERT values. 
        *参数： arg0
        *返回： Object
        *说明： 
        */
        protected Object Insert(string arg0)
        {
            try
            {
                return getSqlMapClient().Insert(arg0, null);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： openConnection
        *功能： Overloaded. Open a connection 
        *参数： 
        *返回： ISqlMapSession
        *说明： 
        */
        protected ISqlMapSession OpenConnection()
        {
            try
            {
                return getSqlMapClient().OpenConnection();
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： openConnection
        *功能： Overloaded. Open a connection 
        *参数： arg0
        *返回： ISqlMapSession
        *说明： 
        */
        protected ISqlMapSession OpenConnection(string arg0)
        {
            try
            {
                return getSqlMapClient().OpenConnection(arg0);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： queryForDictionary
        *功能： Overloaded. Executes the SQL and retuns all rows selected in a map that is keyed 
        *       on the property named in the keyProperty parameter. The value at each key will be 
        *       the value of the property specified in the valueProperty parameter. If valueProperty
        *       is null, the entire result object will be entered. 
        *参数： arg0 , arg1, arg2
        *返回： IDictionary
        *说明： 
        */
        protected IDictionary QueryForDictionary(string arg0, Object arg1, string arg2)
        {
            try
            {
                return getSqlMapClient().QueryForDictionary(arg0, arg1, arg2);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： queryForDictionary
        *功能： Overloaded. Executes the SQL and retuns all rows selected in a map that is keyed 
        *       on the property named in the keyProperty parameter. The value at each key will be 
        *       the value of the property specified in the valueProperty parameter. If valueProperty
        *       is null, the entire result object will be entered. 
        *参数： arg0 , arg1, arg2 , arg3
        *返回： IDictionary
        *说明： 
        */
        protected IDictionary QueryForDictionary(string arg0, Object arg1, string arg2, string arg3)
        {
            try
            {
                return getSqlMapClient().QueryForDictionary(arg0, arg1, arg2, arg3);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： queryForList
        *功能： Overloaded. Executes a Sql SELECT statement that returns data to populate
        *       a number of result objects. The parameter object is generally used to supply
        *       the input data for the WHERE clause parameter(s) of the SELECT statement.
        *参数： arg0 , arg1
        *返回： IList
        *说明： 
        */
        protected IList QueryForList(string arg0, Object arg1)
        {
            try
            {
                return getSqlMapClient().QueryForList(arg0,arg1);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： queryForList
        *功能： Overloaded. Executes a Sql SELECT statement that returns data to populate
        *       a number of result objects. The parameter object is generally used to supply
        *       the input data for the WHERE clause parameter(s) of the SELECT statement.
        *参数： arg0 , arg1 , arg2
        *返回： 
        *说明： 
        */
        protected void QueryForList(string arg0, Object arg1, IList arg2)
        {
            try
            {
                this.getSqlMapClient().QueryForList(arg0, arg1, arg2);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
       *名称： queryForList
       *功能： Executes the SQL and retuns all rows selected. The parameter object is generally
       *       used to supply the input data for the WHERE clause parameter(s) of the SELECT statement. 
       *参数： arg0 , arg1 , arg2 , arg3
       *返回： IList
       *说明： 
       */
        protected IList QueryForList(string arg0, Object arg1, Int32 arg2, Int32 arg3)
        {
            try
            {
                return this.getSqlMapClient().QueryForList(arg0, arg1, arg2,arg3);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： queryForMap
        *功能： Executes the SQL and retuns all rows selected in a map that is keyed on
        *       the property named in the keyProperty parameter. The value at each key will be the entire result object. 
        *参数： arg0 , arg1 , arg2
        *返回： IDictionary
        *说明： 
        */
        protected IDictionary QueryForMap(string arg0, Object arg1, string arg2)
        {
            try
            {
                return this.getSqlMapClient().QueryForMap(arg0, arg1, arg2);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： queryForMap
        *功能： Executes the SQL and retuns all rows selected in a map that is keyed on the property
        *       named in the keyProperty parameter. The value at each key will be the value of the 
        *       property specified in the valueProperty parameter. If valueProperty is null, the entire
        *       result object will be entered. 
        *参数： arg0 , arg1 , arg2 , arg3
        *返回： IDictionary
        *说明： 
        */
        protected IDictionary QueryForMap(string arg0, Object arg1, string arg2, string arg3)
        {
            try
            {
                return this.getSqlMapClient().QueryForMap(arg0, arg1, arg2, arg3);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： queryForMapWithRowDelegate
        *功能： Runs a query with a custom object that gets a chance to deal with each row as it is processed.
        *       The parameter object is generally used to supply the input data for the WHERE clause parameter(s)
        *       of the SELECT statement. 
        *参数： arg0 , arg1 , arg2 , arg3 , arg4
        *返回： IDictionary
        *说明： 
        */
        protected IDictionary QueryForMapWithRowDelegate(string arg0, Object arg1, string arg2, string arg3, DictionaryRowDelegate arg4)
        {
            try
            {
                return this.getSqlMapClient().QueryForMapWithRowDelegate(arg0, arg1, arg2, arg3, arg4);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： queryForObject
        *功能： Executes a Sql SELECT statement that returns that returns data to populate a single
        *       object instance. The parameter object is generally used to supply the input data for
        *       the WHERE clause parameter(s) of the SELECT statement 
        *参数： arg0 , arg1
        *返回： object
        *说明： 
        */
        protected object QueryForObject(string arg0, Object arg1)
        {
            try
            {
                return this.getSqlMapClient().QueryForObject(arg0, arg1);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： queryForObject
        *功能： Executes a Sql SELECT statement that returns a single object of the type of
        *       the resultObject parameter.  
        *参数： arg0 , arg1 , arg2
        *返回： object
        *说明： 
        */
        protected object QueryForObject(string arg0, Object arg1, Object arg2)
        {
            try
            {
                return this.getSqlMapClient().QueryForObject(arg0, arg1,arg2);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： queryWithRowDelegate
        *功能： Runs a query for list with a custom object that gets a chance to deal with each row
        *       as it is processed. The parameter object is generally used to supply the input data 
        *       for the WHERE clause parameter(s) of the SELECT statement.   
        *参数： arg0 , arg1 , arg2
        *返回： IList
        *说明： 
        */
        protected IList QueryWithRowDelegate(string arg0, Object arg1, RowDelegate arg2)
        {
            try
            {
                return this.getSqlMapClient().QueryWithRowDelegate(arg0, arg1, arg2);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： rollBackTransaction
        *功能： Overloaded. Rolls back a transaction from a pending state.    
        *参数： 
        *返回： 
        *说明： 
        */
        protected void RollBackTransaction()
        {
            try
            {
                this.getSqlMapClient().RollBackTransaction();
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： rollBackTransaction
        *功能： Rolls back a transaction from a pending state.
        *参数： arg0
        *返回： 
        *说明： 
        */
        protected void RollBackTransaction(Boolean arg0)
        {
            try
            {
                this.getSqlMapClient().RollBackTransaction(arg0);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： toString
        *功能： Returns a String that represents the current Object.
        *参数： 
        *返回： string
        *说明： 
        */
        public override string ToString()
        {
            try
            {
                return this.getSqlMapClient().ToString();
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： update
        *功能： Executes a Sql UPDATE statement. Update can also be used for any other update statement type,
        *       such as inserts and deletes. Update returns the number of rows effected. The parameter object
        *       is generally used to supply the input data for the UPDATE values as well as the WHERE clause 
        *       parameter(s). 
        *参数： arg0 , arg1
        *返回： int
        *说明： 
        */
        protected int Update(string arg0, Object arg1)
        {
            try
            {
                return this.getSqlMapClient().Update(arg0,arg1);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }

        /*
        *名称： commitTransaction
        *功能： Commits the database transaction. 
        *参数： 
        *返回： 
        *说明： 
        */
        protected void CommitTransaction()
        {
            try
            {
                getSqlMapClient().CommitTransaction();
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
	    }

        /*
        *名称： commitTransaction
        *功能： Commits the database transaction. 
        *参数： arg0
        *返回： 
        *说明： 
        */
        protected void CommitTransaction(Boolean arg0)
        {
            try
            {
                getSqlMapClient().CommitTransaction(arg0);
            }
            catch (Exception e)
            {
                getLogger().Fatal(e);
                throw new DataAccessException(e);
            }
        }        
    }
}